//
//  AdderssManageVC.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/2/27.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "AdderssManageVC.h"
#import "AppHeader.h"
#import "AddressCell.h"

#import "EditAdderssVC.h"


@interface AdderssManageVC () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray<AddressModel *> *dataArray;

@end

@implementation AdderssManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.needRefresh = NO;
    self.dataArray = [NSMutableArray array];
    self.tableView.rowHeight =UITableViewAutomaticDimension;
    [self _requestNetwork];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.needRefresh) {
        [self _requestNetwork];
    }
}

#pragma mark - Request Network
- (void)_requestNetwork {
    
    [HttpUrl GET:@"address/selectAllAddress" dict:nil hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            [self.dataArray removeAllObjects];
            for (NSDictionary *dic in data[@"data"]) {
                AddressModel *model = [AddressModel mj_objectWithKeyValues:dic];
                if (model.level == 1) {
                    [self.dataArray insertObject:model atIndex:0];
                } else {
                    [self.dataArray addObject:model];
                }
            }
            [self.tableView reloadData];
        } else {
            ShowAlertTitle(data[@"msg"]);
        }
    }];
    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 10)];
    headerView.backgroundColor =tableView.backgroundColor;
    return headerView;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
    footerView.backgroundColor =[UIColor whiteColor];
    
    return footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddressCell *cell =[tableView dequeueReusableCellWithIdentifier:@"AddressCell"];
    if (!cell) {
        cell =[AddressCell findCellWithIdentifier:@"AddressCell"];
        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
    }

    AddressModel *model = self.dataArray[indexPath.row];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@", model.recipients];
    cell.phoneLabel.text = [NSString stringWithFormat:@"%@", model.recipientsPhone];
    cell.typeLabel.hidden = model.level ? NO : YES;
    cell.adderssLabel.text = [NSString stringWithFormat:@"%@", model.address];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.chooseBlock) {
        [self.navigationController popViewControllerAnimated:YES];
        self.chooseBlock(self.dataArray[indexPath.row]);
    } else {
        EditAdderssVC *VC =[[EditAdderssVC alloc] init];
        VC.title =@"修改地址";
        VC.addressModel = self.dataArray[indexPath.row];
        [VC addOrEditSuccessBlock:^(BOOL isSuccess, BOOL isEdit) {
            [self _requestNetwork];
        }];
        [self.navigationController pushViewController:VC animated:YES];
    }
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle ==UITableViewCellEditingStyleDelete) {
        NSLog(@"删除点击=%ld", indexPath.row);
        [HttpUrl GET:@"address/deleteById"dict:@{@"id":@(self.dataArray[indexPath.row].id)} hud:self.view isShow:YES WithSuccessBlock:^(id data) {
            if (BB_isSuccess) {
                [self.dataArray removeObjectAtIndex:indexPath.row];
                [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            } else {{
                
                ShowAlertTitle(data[@"msg"]);
            }}
        }];
        
        
        
    }
}

- (IBAction)addButtonClick:(UIButton *)sender {
    EditAdderssVC *VC =[[EditAdderssVC alloc] init];
    VC.title =@"新增收货地址";
    [VC addOrEditSuccessBlock:^(BOOL isSuccess, BOOL isEdit) {
        [self _requestNetwork];
    }];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
